<?php

declare(strict_types=1);

namespace App\Controller;

use App\HscoinConf;
use App\Service\FundCacheService;
use App\Service\FundConfService;
use App\Service\FundBillService;
use App\Service\HscoinBillService;
use App\Service\HscoinBalanceService;

use App\Service\HscoinConfService;
use App\Service\HscoinExchangeService;
use App\Service\HscoinStatsService;
use App\Service\PriceLogService;
use function Hyperf\Config\config;
use function Hyperf\Support\env;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;


class HscoinController extends AbstractController
{

    #[Inject]
    private HscoinBillService $billService;
    #[Inject]
    private HscoinBalanceService $balanceService;
    #[Inject]
    private HscoinStatsService $statsService;
    #[Inject]
    private HscoinExchangeService $exchangeService;
    #[Inject]
    private HscoinConfService $confService;
    #[Inject]
    private PriceLogService $priceLogService;


    function add()
    {
        $data = $this->request->post();
        $category = (int)$data['category'] ?? 1;
        $link_id = (int)$data['link_id'] ?? 0;
        $amount = (float)$data['amount'] ?? 0;
        $link_amount = (float)$data['link_amount'] ?? 0;
        $mark = (string)$data['mark'] ?? '';
        $uid = (int)$data['uid'] ?? 1;
        $e = $this->request->post('e');

        return $this->jsonSuccess($this->billService->add($category, $link_id, $link_amount, $amount, $uid, $mark));
    }

    function cancel()
    {
        $data = $this->request->post();
        $category = (int)$data['category'] ?? 1;
        $link_id = (int)$data['link_id'] ?? 0;
        $uid = $data['uid'] ?? 0;
        return $this->jsonSuccess($this->billService->cancel($category, $link_id, $uid));
    }

    function getBalance()
    {
        $rs = $this->statsService->getBalance();

        return $this->jsonSuccess(['balance' => $rs]);
    }

    function listUser()
    {
        $page = (int)$this->request->input('page', 1);
        $limit = (int)$this->request->input('limit', 20);
        $type = (int)$this->request->input('type', 0);
        $uid = (int)$this->request->input('uid', 0);
        $category = (int)$this->request->input('category', 0);

        return $this->jsonSuccess($this->billService->listBill($page, $limit, $uid, $category, $type));

    }


    function getList()
    {//$uid = 0, $category = 0, $min_id = 0, $max_id = 0, $size = 10
        $data = $this->request->getQueryParams() ?? [];
        $rs = $this->billService->getList($data['uid'] ?? 0, $data['category'] ?? 0, $data['min_id'] ?? 0, $data['max_id'] ?? 0, $data['size'] ?? 0);
        return $this->jsonSuccess($rs);
    }

    function exchangeMoney()
    {
        $data = $this->request->post();

        $uid = $data['uid'] ?? 0;
        $amount = $data['amount'] ?? 0;
        $hiscoin_price = $this->priceLogService->getPrice();

        if (!$hiscoin_price) {
            return $this->jsonError('获取价格失败');
        }

        if (!$this->exchangeService->checkAmount($uid, $amount)) {
            return $this->jsonError('余额不足');
        }
        $feeRate = $this->confService->get(HscoinConf::FIELD_EXCHANGE_FEE_RATE, 0);
        $rs = $this->exchangeService->exchange($uid, $amount, $hiscoin_price, $feeRate);

        if ($rs) {
            return $this->jsonSuccess($rs);
        } else {
            return $this->jsonError('兑换失败');
        }
    }

    function listBill()
    {
        $page = (int)$this->request->input('page', 1);
        $limit = (int)$this->request->input('limit', 20);
        $type = (int)$this->request->input('type', 0);
        $uid = (int)$this->request->input('uid', 0);
        $category = (int)$this->request->input('category', 0);

        return $this->jsonSuccess($this->billService->listBill($page, $limit, $uid, $category, $type));
    }

    function countIncomeCategory()
    {
        $rs = $this->countCategory(HscoinConf::$categoryIncomeLabelMap);
        return $this->jsonSuccess($rs);
    }

    private function countCategory($categoryLabelMap)
    {
        [$list, $status] = $this->statsService->getCache();
        $map = array_column($list, 'cnt', 'category');
        $rs = [];
        foreach ($categoryLabelMap as $category => $label) {
            $rs[] = [
                'category' => $category,
                'label' => $label,
                'cnt' => $map[$category] ?? 0,
            ];
        }
        return $rs;
    }

    function countExpenseCategory()
    {
        $rs = $this->countCategory(HscoinConf::$categoryExpenseLabelMap);
        return $this->jsonSuccess($rs);
    }

    function getStats()
    {
        return $this->jsonSuccess($this->statsService->getStats());
    }

    function listBalance()
    {
        $page = (int)$this->request->input('page', 1);
        $limit = (int)$this->request->input('limit', 20);
        $type = (int)$this->request->input('type', 0);
        $uid = (int)$this->request->input('uid', 0);
        $category = (int)$this->request->input('category', 0);

        return $this->jsonSuccess($this->balanceService->listBalance($page, $limit, $uid, $category, $type));
    }

    function getMine()
    {
       $rs=(array) $this->balanceService->getMine($this->request->input('uid',0));
       $priceInfo=$this->priceLogService->getPriceInfo();
       return $this->jsonSuccess(array_merge($rs,$priceInfo));
    }
}
